package com.azxc.rapid.auth.utils;

import org.apache.logging.log4j.util.Strings;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author linym
 * @create-day 2018-08-14 11:07
 */
public class MapSort {

    // (首先比较参数名的第一个字母，按abcdefg顺序排列，若遇到相同首字母，则看第二个字母，以此类推)
    public static String toStringMap(Map<String,String> map){

        String[] tt=new String[map.size()];
        List<String> klist=new LinkedList<>();
        List<String> vlist=new LinkedList<>();
        for(String key : map.keySet()){
            sort(klist,key,vlist,map.get(key), map.size());
        }
        StringBuilder sb=new StringBuilder();
        for(int count=0;count<klist.size();count++){
            sb.append(klist.get(count));
            sb.append("="+vlist.get(count));
            if(count!=tt.length-1){
                sb.append("&");
            }
        }
        //System.out.println("mapSort:"+sb.toString());
        return sb.toString();
    }


    public static void sort(String[] a,String str){
        char[] ch = str.toCharArray();
        for(int i=0;i<a.length;i++){
            if(a[i]==null){
                a[i]=str;
                return;
            }
            char[] tch=a[i].toCharArray();
            int count=tch.length<=ch.length?tch.length:ch.length;
            for(int j=0;j<count;j++){
                if(ch[j]<tch[j]){
                    rebuild(a,str,i);
                    break;
                }
                if(ch[j]>tch[j]){
                    if((i+1<=a.length-1)&& Strings.isNotBlank(a[i+1])){
                        a[i+1]=str;
                    }
                    break;
                }
            }
        }
    }

    public static void sort(List<String>list,String str,List vlist,String value,int size){
        char[] ch = str.toCharArray();
        for(int i=0;i<size;i++){
            if(i+1>list.size()){
                list.add(str);
                vlist.add(value);
                return;
            }
            char[] tch=list.get(i).toCharArray();
            int count=tch.length<=ch.length?ch.length:tch.length;
            for(int j=0;j<count;j++){
                //同样的情况，短的已经没了
                if(j>=tch.length){
                    list.add(i,str);
                    vlist.add(i,value);
                    return;
                }
                if(j>=ch.length){
                    break;
                }
                if(ch[j]<tch[j]){
                    list.add(i,str);
                    vlist.add(i,value);
                    return;
                }
                if(ch[j]>tch[j]){
                    break;
                }
            }
        }
    }
    //8  ss 2
    public static void rebuild(String[] str,String insertStr,int index){
        for(int i=str.length-1;i>=index;i--){
            if(i-1>=0&&Strings.isNotBlank(str[i-1])){
                str[i]=str[i-1];
            }
            if(i==index){
                str[i]=insertStr;
                break;
            }
            if(i==0){
                break;
            }
        }
    }

    /**
     * 根据value 倒序排序
     * @param map
     * @param <K>
     * @param <V>
     * @return
     */
    public static  <K, V extends Comparable<? super V>> Map<K, V> sortByValueReversed(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap<>();

        map.entrySet().stream()
                .sorted(Map.Entry.<K, V>comparingByValue()
                        .reversed()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        return result;
    }

    /**
     * 根据key 倒序排序
     * @param map
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K extends Comparable<? super K>, V > Map<K, V> sortByKeyReversed(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap<>();

        map.entrySet().stream()
                .sorted(Map.Entry.<K, V>comparingByKey()
                        .reversed()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        return result;
    }

    /**
     * 根据key 正序
     * @param map
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K extends Comparable<? super K>, V > Map<K, V> sortByKey(Map<K, V> map) {
        Map<K, V> result = new LinkedHashMap<>();

        map.entrySet().stream()
                .sorted(Map.Entry.<K, V>comparingByKey()
                        ).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        return result;
    }

}
